<?php


class VodEpisode extends CActiveRecord
{
	const EPISODE_STATUS_ACTIVE = 1;
	const EPISODE_STATUS_PENDING = 2;
	const EPISODE_STATUS_REJECTED = 0;
	
	public $Vzcjo3lxkmbg;
	public $Vmabqjxffnbi;
	public $Viw1cij4sn3f;
	public $Vvzhqgqf4t4p;
	public $V0b1ihlordvk;
	public $Var5itcrymel;
	public $Vrwxpiewut2t;
	public $V54a4mp5olw3;
	public $Vmmznaeztrln;
	
	
	public static function model($Vgmajjibziuy=__CLASS__)
	{
		return parent::model($Vgmajjibziuy);
	}

	
	public function tableName()
	{
		return 'vod_episode';
	}

	
	public function rules()
	{
		
		
		return array(
			array('vod_asset_id, code_name, display_name, is_multibitrate', 'required'),
			array('vod_asset_id, status, episode_order, is_free, view_count, comment_count, favorite_count, vod_stream_count, like_count, dislike_count, duration, is_multibitrate, rating_count, using_duration', 'numerical', 'integerOnly'=>true),
			array('price, rating, price_download, price_gift', 'numerical'),
			array('code_name, display_name', 'length', 'max'=>200),
			array('tags', 'length', 'max'=>500),
			array('create_user_id, modify_user_id', 'length', 'max'=>11),
			array('description, create_date, modify_date, approve_date', 'safe'),
			
			
			array('id, vod_asset_id, code_name, display_name, status, description, tags, episode_order, price, is_free, create_date, create_user_id, modify_date, modify_user_id, view_count, comment_count, favorite_count, vod_stream_count, like_count, dislike_count, duration, rating, is_multibitrate, price_download, price_gift, rating_count, using_duration, approve_date', 'safe', 'on'=>'search'),
		);
	}

	
	public function relations()
	{
		
		
		return array(
			'downloadTokens' => array(self::HAS_MANY, 'DownloadToken', 'vod_episode_id'),
			'subscriberTransactions' => array(self::HAS_MANY, 'SubscriberTransaction', 'vod_episode_id'),
			'viewTokens' => array(self::HAS_MANY, 'ViewToken', 'vod_episode_id'),
			'vodAsset' => array(self::BELONGS_TO, 'VodAsset', 'vod_asset_id'),
			'createUser' => array(self::BELONGS_TO, 'AppUser', 'create_user_id'),
			'modifyUser' => array(self::BELONGS_TO, 'AppUser', 'modify_user_id'),
			'vodImages' => array(self::HAS_MANY, 'VodImage', 'vod_episode_id'),
			'vodStreams' => array(self::HAS_MANY, 'VodStream', 'vod_episode_id'),
			'vodSubscriberMappings' => array(self::HAS_MANY, 'VodSubscriberMapping', 'vod_episode_id'),
		);
	}

	
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'vod_asset_id' => 'Tên series',
			'code_name' => 'Code Name',
			'display_name' => 'Tên tập phim',
			'status' => 'Trạng thái',
			'description' => 'Mô tả',
			'tags' => 'Tags',
			'episode_order' => 'Thứ tự tập',
			'price' => 'Giá',
			'is_free' => 'Miễn phí',
			'create_date' => 'Ngày tạo',
			'create_user_id' => 'Create User',
			'modify_date' => 'Ngày sửa',
			'modify_user_id' => 'Modify User',
			'view_count' => 'Lượt xem',
			'comment_count' => 'Lượt comment',
			'favorite_count' => 'Lượt yêu thích',
			'vod_stream_count' => 'Vod Stream Count',
			'like_count' => 'Like Count',
			'dislike_count' => 'Dislike Count',
			'duration' => 'Thời lượng',
			'rating' => 'Rating',
			'is_multibitrate' => 'Is Multibitrate',
			'price_download' => 'Price Download',
			'price_gift' => 'Price Gift',
			'rating_count' => 'Rating Count',
			'using_duration' => 'Using Duration',
			'approve_date' => 'Approve Date',
			'episodeFileName_low' => 'File chất lượng thấp',
			'episodeFileName_normal' => 'File chất lượng bình thường',
			'episodeFileName_high' => 'File chất lượng cao',
			'episodeFrom' => 'Từ tập',
			'episodeTo' => 'Đến tập',
		);
	}

	
	public function search()
	{
		
		

		$V0v5vsnfxezw=new CDbCriteria;

		$V0v5vsnfxezw->compare('id',$this->id);
		$V0v5vsnfxezw->compare('vod_asset_id',$this->vod_asset_id);
		$V0v5vsnfxezw->compare('code_name',$this->code_name,true);
		$V0v5vsnfxezw->compare('display_name',$this->display_name,true);
		$V0v5vsnfxezw->compare('status',$this->status);
		$V0v5vsnfxezw->compare('description',$this->description,true);
		$V0v5vsnfxezw->compare('tags',$this->tags,true);
		$V0v5vsnfxezw->compare('episode_order',$this->episode_order);
		$V0v5vsnfxezw->compare('price',$this->price);
		$V0v5vsnfxezw->compare('is_free',$this->is_free);
		$V0v5vsnfxezw->compare('create_date',$this->create_date,true);
		$V0v5vsnfxezw->compare('create_user_id',$this->create_user_id,true);
		$V0v5vsnfxezw->compare('modify_date',$this->modify_date,true);
		$V0v5vsnfxezw->compare('modify_user_id',$this->modify_user_id,true);
		$V0v5vsnfxezw->compare('view_count',$this->view_count);
		$V0v5vsnfxezw->compare('comment_count',$this->comment_count);
		$V0v5vsnfxezw->compare('favorite_count',$this->favorite_count);
		$V0v5vsnfxezw->compare('vod_stream_count',$this->vod_stream_count);
		$V0v5vsnfxezw->compare('like_count',$this->like_count);
		$V0v5vsnfxezw->compare('dislike_count',$this->dislike_count);
		$V0v5vsnfxezw->compare('duration',$this->duration);
		$V0v5vsnfxezw->compare('rating',$this->rating);
		$V0v5vsnfxezw->compare('is_multibitrate',$this->is_multibitrate);
		$V0v5vsnfxezw->compare('price_download',$this->price_download);
		$V0v5vsnfxezw->compare('price_gift',$this->price_gift);
		$V0v5vsnfxezw->compare('rating_count',$this->rating_count);
		$V0v5vsnfxezw->compare('using_duration',$this->using_duration);
		$V0v5vsnfxezw->compare('approve_date',$this->approve_date,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$V0v5vsnfxezw,
		));
	}

	public static function findEpisodes($Vceaol1c1qxm, $Vtkshvgl1ivp, $Vfyl0i5chevf, $Vtebq5vsa45o, $Vmq5toeun05t, $V5as2qkxgfs4 = -1, $Vrlyvhau3qpi = 1) {
		$V24noxecbnll = "1";
		if($V5as2qkxgfs4 != -1) {
			$V24noxecbnll .= " AND vod_asset_id = ".$V5as2qkxgfs4;
		}
		if($Vrlyvhau3qpi > -1) {
			$V24noxecbnll .= " AND (status = ".$Vrlyvhau3qpi.")";
		}
		if (!empty ($Vmq5toeun05t)) {
			$V24noxecbnll .= " AND (t.display_name LIKE '%$Vmq5toeun05t%' OR t.display_name_ascii LIKE '%$Vmq5toeun05t%') ";
		}

		if ($Vceaol1c1qxm != null) {
			$Vfcisoqdzruw = VodCategory::model()->findByPk($Vceaol1c1qxm)->path;
			$V24noxecbnll .= " AND id in (select vod_asset_id from  vod_category_asset_mapping where vod_category_id in (".
                           "select id from vod_category where concat('/',path,'/') like '/$Vfcisoqdzruw/%')) ";
        	}
        
        
        $Vxun03s52uac = Yii::app()->db->createCommand()
                ->selectDistinct('t.id,t.create_date,t.view_count,t.display_name,t.status')
                ->from('vod_episode as t')
                ->where($V24noxecbnll) 
                ->limit($Vtebq5vsa45o, $Vfyl0i5chevf * $Vtebq5vsa45o)
                ->order($Vtkshvgl1ivp)
                ->queryAll();
        $Vfx3dng1y1f3 = Yii::app()->db->createCommand()
                ->select('count(id)')
                ->from('vod_episode as t')
                ->where($V24noxecbnll) 
                ->queryScalar();
        
        return array(
            'total_result'=>$Vfx3dng1y1f3,
            'keyword'=>$Vmq5toeun05t,
            'page_number'=>$Vfyl0i5chevf,
            'page_size'=>$Vtebq5vsa45o,
            'category'=>$Vceaol1c1qxm,
            'total_page'=>($Vfx3dng1y1f3 - ($Vfx3dng1y1f3 % $Vtebq5vsa45o))/$Vtebq5vsa45o + (($Vfx3dng1y1f3 % $Vtebq5vsa45o ===0)?0:1),
            'data'=>$Vxun03s52uac
        );
    }
}